/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: ReplyReflectFileOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.reflect.autohome.reply 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年8月1日 下午7:24:35 
 * @version: V1.0   
 */
package com.zizo.reflect.autohome.reply;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.zizo.core.ReflectFile;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: ReplyReflectFileOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年8月1日 下午7:24:35
 */
public class ReplyReflectFileOfAutoHome extends ReflectFile {

	/**
	 * @Title:ReplyReflectFileOfAutoHome
	 * @Description:
	 * @param id
	 */
	public ReplyReflectFileOfAutoHome(String id) {
		super(id);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see com.zizo.core.ReflectFile#run()
	 */
	@Override
	public void run() {
		String url = "http://i.service.autohome.com.cn/clubapp/OtherReply-" + this.id + "-1.html";
		Document doc = DocUtil.getDoc(url);
		if (doc == null || doc.select("h1").text().contains("Server Error in '/clubapp' Application.")) {
			return;
		} else {
			Elements spans = doc.select("span.fcolor_6");
			int pages = 1;
			if (spans.size() > 0) {
				if (Integer.parseInt(spans.first().text()) > 20) {
					Elements pagesItems = doc.select("div.paging a");
					pages = Integer.parseInt(pagesItems.get(pagesItems.size() - 2).text());
				}
			} else {
				return;
			}
			for (int i = 1; i < pages + 1; i++) {
				String link = "http://i.service.autohome.com.cn/clubapp/OtherReply-" + this.id + "-" + i + ".html";
				Document docOther = null;
				if (i == 1) {
					docOther = doc;
				} else {
					docOther = DocUtil.getDoc(link);
					if (docOther == null) {
						System.out.println(link);
						continue;
					}
				}
				Util.downloadWebsite("D:/var/server/reflectfile/autohome/reply/" + this.id, i + ".html", link);
			}
		}

	}

}
